package com.ruoyi.system.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.usb.UsbException;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.DocumentVo;
import com.ruoyi.system.domain.bo.DocumentBo;
import com.ruoyi.system.service.IDocumentService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文件信息
 *
 * @author ruoyi
 * @date 2024-06-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/document")
public class DocumentController extends BaseController {

    private final IDocumentService iDocumentService;

    /**
     * 查询文件信息列表
     */
    @SaCheckPermission("system:document:list")
    @GetMapping("/list")
    public TableDataInfo<DocumentVo> list(DocumentBo bo, PageQuery pageQuery) {
        return iDocumentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出文件信息列表
     */
    @SaCheckPermission("system:document:export")
    @Log(title = "文件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DocumentBo bo, HttpServletResponse response) {
        List<DocumentVo> list = iDocumentService.queryList(bo);
        ExcelUtil.exportExcel(list, "文件信息", DocumentVo.class, response);
    }

    /**
     * 获取文件信息详细信息
     *
     * @param documentId 主键
     */
    @SaCheckPermission("system:document:query")
    @GetMapping("/{documentId}")
    public R<DocumentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long documentId) {
        return R.ok(iDocumentService.queryById(documentId));
    }

    /**
     * 新增文件信息
     */
    @SaCheckPermission("system:document:add")
    @Log(title = "文件信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DocumentBo bo) {
        return toAjax(iDocumentService.insertByBo(bo));
    }

    /**
     * 修改文件信息
     */
    @SaCheckPermission("system:document:edit")
    @Log(title = "文件信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DocumentBo bo) {
        return toAjax(iDocumentService.updateByBo(bo));
    }

    /**
     * 删除文件信息
     *
     * @param documentIds 主键串
     */
    @SaCheckPermission("system:document:remove")
    @Log(title = "文件信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{documentIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] documentIds) {
        return toAjax(iDocumentService.deleteWithValidByIds(Arrays.asList(documentIds), true));
    }

    @GetMapping("splitFileAndSend")
    public R splitFileAndSend(String fileName) {
        boolean result = iDocumentService.splitFileAndSend(fileName);
        if(result) {
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @GetMapping("usb")
    public R testUSB() throws UsbException, UnsupportedEncodingException {
        String count = iDocumentService.testUSB();
        return R.ok(count);
    }
}
